package com.meixiang.util;

import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.meixiang.main.MXApplication;

/**
 * 软键盘工具类
 *
 * @author LZRUI
 */
public class KeyBoardUtils {
    public static void showKeyBoard(final EditText editText) {
        if (editText == null) {
            return;
        }
        editText.requestFocus();
        editText.post(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getAppContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
            }
        });
    }

    public static void hideSoftInput(EditText editText) {
        if (editText == null) {
            return;
        }
        InputMethodManager inputMethodManager = (InputMethodManager) getAppContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(),
                InputMethodManager.RESULT_UNCHANGED_SHOWN);
    }

    private static Context getAppContext() {
        return MXApplication.mApp;
    }
}
